import pretty_midi
import torch


def get_avg_velocity(instrument):
    # 平均力度
    velocity = 0
    for note in instrument.notes:
        velocity += note.velocity
    velocity /= len(instrument.notes)
    return velocity


def get_sum_duration(instrument):
    # 所有音符累加总时值
    duration = 0
    for note in instrument.notes:
        duration += note.end - note.start
    return duration


def get_types_of_duration(instrument):
    # 时值类型
    duration = []
    for note in instrument.notes:
        duration.append(note.end - note.start)
    types = len(set(duration))
    return types


def get_pitch_range(instrument):
    # 最高音与最低音之间的音程
    pitch = []
    for note in instrument.notes:
        pitch.append(note.pitch)
    sorted_pitch = sorted(set(pitch))

    range = sorted_pitch[-1] - sorted_pitch[0]
    return range


def get_second_pitch_range(instrument):
    # 第二高音与第二低音之间的音程
    pitch = []
    for note in instrument.notes:
        pitch.append(note.pitch)
    sorted_pitch = sorted(set(pitch))

    range = sorted_pitch[-2] - sorted_pitch[1]
    return range

